﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using DocumentManagement.Models;
using System.Windows;
using System.Windows.Data;

namespace DocumentManagement.ViewsModels
{
  public  class ShowItemCreatedocumentViewsModel : ViewsModelBase
    {

        private QSHCEntities1 _entityModels;
        public QSHCEntities1 EntityModels
        {
            get { return _entityModels; }
            set
            {
                if (_entityModels != value)
                {
                    _entityModels = value;
                    OnPropertyChanged("EntityModels");
                }
            }
        }
        private ICollectionView _myCreateDocumentICV;
        public ICollectionView MyCreateDocumentICV
        {
            get { return _myCreateDocumentICV; }
            set
            {
                if (_myCreateDocumentICV != value)
                {
                    _myCreateDocumentICV = value;
                    OnPropertyChanged("MyCreateDocumentICV");
                }
            }
        }

        private CreateSend _myCreateSend;
        public CreateSend MyCreateSend
        {
            get { return _myCreateSend; }
            set
            {
                if (_myCreateSend != value)
                {
                    _myCreateSend = value;
                    OnPropertyChanged("MyCreateSend");
                }
            }
        }

        private CreateSend _mySelectedEditItemCreateSend;
        public CreateSend MySelectedEditItemCreateSend
        {
            get { return _mySelectedEditItemCreateSend; }
            set
            {
                if (_mySelectedEditItemCreateSend != value)
                {
                    _mySelectedEditItemCreateSend = value;
                    OnPropertyChanged("MySelectedEditItemCreateSend");
                }
            }
        }

        public ShowItemCreatedocumentViewsModel()
        {
            if (!DesignerProperties.GetIsInDesignMode(new DependencyObject()))
            {
                EntityModels = new QSHCEntities1();

                MyCreateDocumentICV = CollectionViewSource.GetDefaultView(EntityModels.CreateSends.Where(p => p.Active == true).Where(p => p.Paragraph1 != null).ToList()); //.Where(p => p.Active == true)

            }
        }

        public void DeleteFromDatabase()
        {

            var dr = MessageBox.Show("ต้องการลบข้อมูลหรือไม่", "ยืนยันการลบข้อมูล", MessageBoxButton.OKCancel, MessageBoxImage.Warning, MessageBoxResult.OK);
            if (dr == MessageBoxResult.OK)
            {
                var deleteCreateSend = (EntityModels.CreateSends.Where(p => p.CreateSendID == MySelectedEditItemCreateSend.CreateSendID).Single());
                deleteCreateSend.Active = false;
                //var deleteSend = (EntityModels.CreateSends.Where(p => p.CreateSendCode == MySelectedEditItemCreateSend.CreateSendCode).Single());
                //deleteSend.Active = false;

                foreach (var item in EntityModels.CreateSendAttacheds)
                {
                    if (item.CreateSendID == MySelectedEditItemCreateSend.CreateSendID)
                    {
                        item.Active = false;

                    }
                }
                EntityModels.SaveChanges();

                MyCreateDocumentICV = CollectionViewSource.GetDefaultView(EntityModels.CreateSends.Where(p => p.Active == true).Where(p => p.Paragraph1 != null).ToList());
                MyCreateDocumentICV.Refresh();


            }
        }
        //public void RefreshData()
        //{
        //    MyCreateDocumentICV = CollectionViewSource.GetDefaultView(EntityModels.CreateSends.Where(p => p.Active == true).ToList());
        //}


        public void Filter(string txtFilter, DateTime? startdate, DateTime? enddate)
        {

            MyCreateDocumentICV = CollectionViewSource.GetDefaultView(EntityModels.CreateSends.Where(p => p.Active == true).ToList());


            MyCreateDocumentICV.Filter += (o) =>
            {
                CreateSend p = (o as CreateSend);

                if (p == null)

                    return false;

                if (txtFilter != "" && startdate == null && enddate == null)
                {
                    if (p.CreateSendCode.Contains(txtFilter) || p.Title.Contains(txtFilter))
                        return true;
                    else
                        return false;
                }

                if (startdate != null && enddate != null && txtFilter == "")
                {
                    if (p.CreateSendDate.Date >= startdate.Value.Date && p.CreateSendDate.Date <= enddate.Value.Date)
                        return true;
                    else
                        return false;
                }

                if (startdate != null && enddate == null && txtFilter == "")
                {
                    if (p.CreateSendDate.Date >= startdate.Value.Date)
                        return true;
                    else
                        return false;
                }

                if (enddate != null && startdate == null && txtFilter == "")
                {
                    if (p.CreateSendDate.Date <= enddate.Value.Date)
                        return true;
                    else
                        return false;
                }

                if (txtFilter != "" && startdate != null && enddate == null)
                {
                    if ((p.CreateSendCode.Contains(txtFilter) || p.Title.Contains(txtFilter)) && p.CreateSendDate.Date >= startdate.Value.Date)
                        return true;
                    else
                        return false;
                }

                if (txtFilter != "" && startdate == null && enddate != null)
                {
                    if ((p.CreateSendCode.Contains(txtFilter) || p.Title.Contains(txtFilter)) && p.CreateSendDate.Date <= enddate.Value.Date)
                        return true;
                    else
                        return false;
                }

                if (startdate != null && enddate != null && txtFilter != "")
                {

                    if ((p.CreateSendCode.Contains(txtFilter) || p.Title.Contains(txtFilter)) && (p.CreateSendDate.Date >= startdate.Value.Date && p.CreateSendDate.Date <= enddate.Value.Date))
                        return true;
                    else
                        return false;

                }

                return true;
            
            };
        }


        public void GetAutoNumber()
        {
            string year = Convert.ToString(DateTime.Today.Year + 543);

            try
            {

                //foreach (var item in EntityModels.Sends.Where(p => p.Status == 2))
                //{
                //    code = item.SendCode.ToString();

                //    MessageBox.Show(code);

                //}

                string AutoNum = EntityModels.CreateSends.OrderByDescending(p => p.CreateSendCode).Where(p => p.Type == 2).First().CreateSendCode;
                var firstcode = AutoNum.Substring(17, 4);
                var lastcode = Convert.ToInt32(AutoNum.Substring(22, 4));

                if (firstcode != year)
                {
                    MyCreateSend.CreateSendCode = string.Format("{0}/{1}.{2}", "ศธ 0514.7.5.1.10", year, "0001");
                }
                else
                {
                    MyCreateSend.CreateSendCode = string.Format("{0}/{1}.{2}", "ศธ 0514.7.5.1.10", year, (lastcode + 1).ToString("D4"));//digital string 4 ตัว เช่น 0003
                }






            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
                MyCreateSend.CreateSendCode = string.Format("{0}/{1}.{2}", "ศธ 0514.7.5.1.10", year, "0001");
            }


        }

     
    }
}
